#include "addcolumn.h"
#include "ui_addcolumn.h"

AddColumn::AddColumn(QWidget *parent,
                     const QString &table_name,
                     int table_id) :
    QDialog(parent),
    ui(new Ui::AddColumn)
{
    ui->setupUi(this);

    table=REF_COLUMN;
    tableId=table_id;
    ui->tableName->setText(table_name);
}

AddColumn::~AddColumn()
{
    delete ui;
}

bool AddColumn::addItem(const QString &colName, const QString &colLabel)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (table_id,column_name,column_label) values (?,?,?)")
                  .arg(table));
    query.addBindValue(tableId);
    query.addBindValue(colName);
    query.addBindValue(colLabel);
    return query.exec();
}

void AddColumn::on_btn_submit_clicked()
{
    auto name=ui->colName->text().trimmed();
    auto label=ui->colLabel->text().trimmed();
    if(name.isEmpty() || label.isEmpty())
        return;

    if(addItem(name,label))
        accept();
    else {
        QMessageBox::warning(this,"异常情况","无法提交数据!");
    }
}

void AddColumn::on_btn_copy_clicked()
{
    auto name=ui->tableName->text();
    qApp->clipboard()->setText(name);
    QMessageBox::information(this,"操作提示", name+"已复制到剪贴板");
}
